package com.zhida.jiyangcenter.setting

import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.utils.startActivity
import chooongg.base.utils.tipFail
import chooongg.http.APIException
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.account.ForgetPhoneActivity
import com.zhida.jiyangcenter.account.SetPassActivity
import com.zhida.jiyangcenter.api.APIAccount
import com.zhida.jiyangcenter.api.BaseAPI
import com.zhida.jiyangcenter.api.PersonalInfoEntity
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.request
import kotlinx.android.synthetic.main.activity_account_safety.*

@TitleBar(false)
@ContentLayout(R.layout.activity_account_safety)
class AccountSafetyActivity : BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.title("账户与安全").navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        getData()
    }

    private fun getData() {
        showTipLoading()
        lifecycleScope.request({ APIAccount.service().personalInfo() },
            object : BaseAPI<PersonalInfoEntity>() {
                override fun onSuccess(t: PersonalInfoEntity?, state: Int, msg: String) {
                    hideTipLoading()
                    if (t?.password == true) {
                        btn_account_safety.setOnClickListener {
                            startActivity<ForgetPhoneActivity> {  }
                        }
                        tv_account_safety.text="登录密码修改"
                    }else{
                        tv_account_safety.text="登录密码设置"
                        btn_account_safety.setOnClickListener {
                            startActivity<SetPassActivity> {
                                putExtra("type","account")
                            }
                        }
                    }
                    if (t?.pay_password==true){
                        tv_pay_password.text="支付密码修改"
                        btn_pay_password.setOnClickListener {
                            startActivity<SetPayPasswordActivity> {
                                putExtra("type","editpws")
                            }
                        }
                    }else{
                        btn_pay_password.setOnClickListener {
                            startActivity<SetPayPasswordActivity> {
                                putExtra("type","add")
                            }
                        }
                        tv_pay_password.text="支付密码设置"
                    }
                }

                override fun onError(e: APIException) {
                    tipFail(e.eMessage)
                    hideTipLoading()
                }

            })
    }
}